package com.teacher.pc.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.teacher.common.CommonResult;
import com.teacher.mapper.TermMapper;
import com.teacher.pojo.dto.absentclass.AbsentClassAddDto;
import com.teacher.pojo.dto.absentclass.AbsentClassGetDto;
import com.teacher.pojo.dto.absentclass.AbsentClassUpdateDto;
import com.teacher.pojo.entity.AbsentClass;
import com.teacher.pojo.vo.absentclass.AbsentClassGetVo;
import com.teacher.service.IAbsentClassService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 旷课 前端控制器
 * </p>
 *
 * @author zfm
 * @since 2021-08-01
 */
@RestController
@RequestMapping("/pc/absent-class")
@Api(tags = "旷课")
public class AbsentClassController {

    @Autowired
    private IAbsentClassService iAbsentClassService;

    @Autowired
    private TermMapper termMapper;

    /**
     * 添加旷课
     *
     * @param dto
     * @return
     */
    @ApiOperation("添加旷课 absentClass_add")
    @PostMapping
    @PreAuthorize("hasAuthority('absentClass_add')")
    public CommonResult<String> add(@RequestBody @Validated AbsentClassAddDto dto) {
        AbsentClass absentClass = new AbsentClass();
        dto.setTermId(termMapper.getCurrentTerm());
        BeanUtils.copyProperties(dto, absentClass);
        if (iAbsentClassService.save(absentClass)) {
            return CommonResult.success("添加成功");
        }
        return CommonResult.failed("添加失败");
    }

    /**
     * 更新旷课
     *
     * @param dto
     * @return
     */
    @ApiOperation("更新旷课 absentClass_update")
    @PutMapping
    @PreAuthorize("hasAuthority('absentClass_update')")
    public CommonResult<String> update(@RequestBody @Validated AbsentClassUpdateDto dto) {
        AbsentClass absentClass = new AbsentClass();
        BeanUtils.copyProperties(dto, absentClass);
        if (iAbsentClassService.updateById(absentClass)) {
            return CommonResult.success("更新成功");
        }
        return CommonResult.failed("更新失败");
    }

    /**
     * 删除旷课
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除旷课 absentClass_delete")
    @PreAuthorize("hasAuthority('absentClass_delete')")
    public CommonResult<String> del(@RequestBody List<Long> ids) {
        if (iAbsentClassService.removeByIds(ids)) {
            return CommonResult.success("删除成功");
        }
        return CommonResult.failed("删除失败");
    }

    /**
     * 查询旷课
     *
     * @param dto
     * @return
     */
    @GetMapping
    @ApiOperation("查询旷课 absentClass_get")
    @PreAuthorize("hasAuthority('absentClass_get')")
    public CommonResult<IPage<AbsentClassGetVo>> getPage(AbsentClassGetDto dto) {
        return iAbsentClassService.getPage(dto);
    }

    /**
     * 导入旷课
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation("导入旷课 absentClass_upload")
    @PreAuthorize("hasAuthority('absentClass_upload')")
    public CommonResult<String> upload(@RequestParam(name = "file") MultipartFile file) {
        return iAbsentClassService.upload(file);
    }

}

